package com.uwlaser.rest;

import com.alibaba.fastjson.JSONObject;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.DeviceMain;
import com.uwlaser.service.DeviceMainService;
import com.uwlaser.service.dto.DeviceMainDto;
import com.uwlaser.service.dto.DeviceMainQueryParam;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;

/**
* @author zk-Qin
* @date 2022-05-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "设备列表管理")
@RequestMapping("/api/deviceMain")
public class DeviceMainController {

    private final DeviceMainService deviceMainService;

    @GetMapping
    @Log("查询设备列表")
    @ApiOperation("查询设备列表")
    @PreAuthorize("@el.check('deviceMain:list')")
    public ResponseEntity query(DeviceMainQueryParam query, Pageable pageable){
        return new ResponseEntity<>(deviceMainService.queryAll(query,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增设备列表")
    @ApiOperation("新增设备列表")
    @PreAuthorize("@el.check('deviceMain:add')")
    public ResponseEntity create(MultipartFile file , String data){
        DeviceMainDto deviceMainDto = JSONObject.parseObject(data, DeviceMainDto.class);
        return new ResponseEntity<>(deviceMainService.insert(file ,deviceMainDto),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改设备列表")
    @ApiOperation("修改设备列表")
    @PreAuthorize("@el.check('deviceMain:edit')")
    public ResponseEntity update(MultipartFile file , String data){
        DeviceMainDto deviceMainDto = JSONObject.parseObject(data, DeviceMainDto.class);
        return new ResponseEntity<>(deviceMainService.updateById(file ,deviceMainDto),HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除设备列表")
    @ApiOperation("删除设备列表")
    @PreAuthorize("@el.check('deviceMain:del')")
    public ResponseEntity delete(@RequestBody Set<Integer> ids) {
        deviceMainService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/monitor")
    @Log("查询设备监控列表")
    @ApiOperation("查询设备监控列表")
    @PreAuthorize("@el.check('deviceMain:list')")
    public ResponseEntity monitor(DeviceMainQueryParam query, Pageable pageable){
        return new ResponseEntity<>(deviceMainService.monitor(query,pageable),HttpStatus.OK);
    }

    /*
    @Log("导出设备列表")
    @ApiOperation("导出设备列表")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('deviceMain:list')")
    public void download(HttpServletResponse response, DeviceMainQueryParam query) throws IOException {
        deviceMainService.download(deviceMainService.queryAll(query), response);
    }*/

}
